﻿using System.Reflection;
using System.Windows.Input;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

using Microsoft.UI.Xaml;

using Windows.ApplicationModel;

using WinTime.Contracts.Services;
using WinTime.Helpers;
using WinTime.Models;
using WinTime.Services;

namespace WinTime.ViewModels;

public class SettingsViewModel : ObservableRecipient
{
    private readonly IThemeSelectorService _themeSelectorService;
    private readonly ISettingConfigService _settingConfigService;

    private ElementTheme _elementTheme;

    public List<KeyValuePair<string, ElementTheme>> ThemeItems = new();
    public KeyValuePair<string, ElementTheme> CurrentTheme => ThemeItems.Find(x => x.Value == ElementTheme);

    public KeyValuePair<string, string> CurrentLang => LanguageItems.Find(x => x.Value == SettingConfig.Language);

    public ElementTheme ElementTheme
    {
        get => _elementTheme;
        set => SetProperty(ref _elementTheme, value);
    }

    private SettingConfigModel _settingConfigModel;
    public SettingConfigModel SettingConfig
    {
        get => _settingConfigModel;
        set => SetProperty(ref _settingConfigModel, value);
    }

    public List<KeyValuePair<string, string>> LanguageItems = new();

    public string AppDisplayName => "AppDisplayName".GetLocalized();

    private string _versionDescription;
    public string VersionDescription
    {
        get => _versionDescription;
        set => SetProperty(ref _versionDescription, value);
    }

    public ICommand SwitchThemeCommand
    {
        get;
    }

    public ICommand UpdateSettingConfigCommand
    {
        get;
    }

    public SettingsViewModel(IThemeSelectorService themeSelectorService, ISettingConfigService settingConfigService)
    {
        _themeSelectorService = themeSelectorService;
        _settingConfigService = settingConfigService;
        _elementTheme = _themeSelectorService.Theme;
        _versionDescription = GetVersionDescription();

        _settingConfigModel = _settingConfigService.SettingConfig;
        SwitchThemeCommand = new RelayCommand<ElementTheme>(
            async (param) =>
            {
                if (ElementTheme != param)
                {
                    ElementTheme = param;
                    await _themeSelectorService.SetThemeAsync(param);
                }
            });
        UpdateSettingConfigCommand = new RelayCommand<SettingConfigModel>(
            async (param) =>
            {
                if (param != null)
                {
                    await _settingConfigService.SetSettingConfigAsync(param);
                }
            });
        ThemeItems.Add(new KeyValuePair<string, ElementTheme>("Settings_Theme_Default".GetLocalized(), ElementTheme.Default));
        ThemeItems.Add(new KeyValuePair<string, ElementTheme>("Settings_Theme_Light".GetLocalized(), ElementTheme.Light));
        ThemeItems.Add(new KeyValuePair<string, ElementTheme>("Settings_Theme_Dark".GetLocalized(), ElementTheme.Dark));

        LanguageItems.Add(new KeyValuePair<string, string>("简体中文", "zh-Hans-CN"));
        LanguageItems.Add(new KeyValuePair<string, string>("English", "en"));
    }

    private static string GetVersionDescription()
    {
        Version version;

        if (RuntimeHelper.IsMSIX)
        {
            var packageVersion = Package.Current.Id.Version;

            version = new(packageVersion.Major, packageVersion.Minor, packageVersion.Build, packageVersion.Revision);
        }
        else
        {
            version = Assembly.GetExecutingAssembly().GetName().Version!;
        }

        return $"{"AppDisplayName".GetLocalized()} - {version.Major}.{version.Minor}.{version.Build}.{version.Revision}";
    }


}
